package cc.orange.mainView;

import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;

import android.util.Log;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import androidx.fragment.app.FragmentActivity;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.viewpager2.widget.ViewPager2;
import cc.orange.base.BaseFragment;
import cc.orange.entity.HightEntity;

import cc.orange.databinding.FragmentDiscoverBinding;

import cc.orange.R;
//import com.tendcloud.tenddata.TalkingDataSDK;

public class DiscoverFragment extends BaseFragment {

    private List<Fragment> fragments=new ArrayList<>();
    private MyFragmentAdapet2 myFragmentAdapet;
    private FragmentDiscoverBinding binding;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater,R.layout.fragment_discover,container,false);

        EventBus.getDefault().register(this);
        initViews();
        return binding.getRoot();
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    private void initViews() {
        fragments.add(new Discover2Fragment());
        fragments.add(new Discover1Fragment(1,""));

        myFragmentAdapet=new MyFragmentAdapet2(getActivity(),fragments);
        binding.discViewp.setAdapter(myFragmentAdapet);
        binding.discViewp.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                super.onPageScrolled(position, positionOffset, positionOffsetPixels);
            }

            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                if(position == 0){
                    binding.discText1.setTextSize(TypedValue.COMPLEX_UNIT_SP,21);
                    binding.discText2.setTextSize(TypedValue.COMPLEX_UNIT_SP,19);
                    binding.discText1.setTypeface(Typeface.DEFAULT_BOLD);
                    binding.discText2.setTypeface(Typeface.DEFAULT);
                    binding.discRel1.setVisibility(View.GONE);
                    binding.discRel2.setVisibility(View.VISIBLE);
                }else {
                    binding.discText2.setTextSize(TypedValue.COMPLEX_UNIT_SP,21);
                    binding.discText1.setTextSize(TypedValue.COMPLEX_UNIT_SP,19);
                    binding.discText1.setTypeface(Typeface.DEFAULT);
                    binding.discText2.setTypeface(Typeface.DEFAULT_BOLD);
                    binding.discRel2.setVisibility(View.GONE);
                    binding.discRel1.setVisibility(View.VISIBLE);
                }
            }
        });

        binding.discText1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                binding.discText1.setTextSize(TypedValue.COMPLEX_UNIT_SP,23);
                binding.discText1.setTypeface(Typeface.DEFAULT_BOLD);
                binding.discText2.setTypeface(Typeface.DEFAULT);
                binding.discText2.setTextSize(TypedValue.COMPLEX_UNIT_SP,20);
                binding.discRel1.setVisibility(View.GONE);
                binding.discRel2.setVisibility(View.VISIBLE);
                binding.discViewp.setCurrentItem(0);
            }
        });

        binding.discText2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                binding.discText2.setTextSize(TypedValue.COMPLEX_UNIT_SP,23);
                binding.discText1.setTypeface(Typeface.DEFAULT);
                binding.discText2.setTypeface(Typeface.DEFAULT_BOLD);
                binding.discText1.setTextSize(TypedValue.COMPLEX_UNIT_SP,20);
                binding.discRel2.setVisibility(View.GONE);
                binding.discRel1.setVisibility(View.VISIBLE);
                binding.discViewp.setCurrentItem(1);
            }
        });
        binding.rlSubmit.setOnClickListener(view -> {
            startActivity(new Intent(getActivity(),SendDynamicActivity.class));
        });

    }


    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void getHight(HightEntity entity) {//改变布局
        Log.v("---->3",""+entity.getHight());
        if(entity.getHight() > 0){
            RelativeLayout.LayoutParams pp = new RelativeLayout.
                    LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                    entity.getHight());
            binding.homeTitle.setLayoutParams(pp);
        }
    }


    @Override
    public void onResume() {
        super.onResume();
//        TalkingDataSDK.onPageBegin(getActivity().getApplicationContext(), this.toString());
    }

    @Override
    public void onPause() {
        if(!this.isHidden()){
//            TalkingDataSDK.onPageEnd(getActivity().getApplicationContext(), this.toString());
        }
        super.onPause();
    }
}

class MyFragmentAdapet2 extends FragmentStateAdapter {
    private List<Fragment> fragments;
    public MyFragmentAdapet2(@NonNull FragmentActivity fragmentActivity, List<Fragment> fragments) {
        super(fragmentActivity);
        this.fragments=fragments;
    }

    @NonNull
    @Override
    public Fragment createFragment(int position) {
        return fragments.get(position);
    }

    @Override
    public int getItemCount() {
        return fragments.size();
    }

}